package cn.bone.zk.zk03;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LockTest {

    public static void main(String[] args) {

        DistributedLock lock1 = new DistributedLock();
        DistributedLock lock2 = new DistributedLock();
        DistributedLock lock3 = new DistributedLock();

        new Thread(new LockThread(lock1)).start();
        new Thread(new LockThread(lock2)).start();
        new Thread(new LockThread(lock3)).start();

    }

    public static class LockThread implements Runnable{

        private DistributedLock lock;
        private Logger logger = LoggerFactory.getLogger(LockThread.class);

        public LockThread(DistributedLock lock){
            this.lock = lock;
        }
        @Override
        public void run() {
            lock.zkLock();
            logger.info(Thread.currentThread().getName() + "启动，获取到了锁");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.zkUnlock();
            logger.info(Thread.currentThread().getName() + "释放了锁");
        }
    }
}

